package com.koushare.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class RequestAndResponseTool {
    private static RequestConfig requestConfig = null;

    static {
        // 设置请求和传输超时时间
        requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).setConnectionRequestTimeout(10000).build();
    }


    public static Page sendRequstAndGetResponse(String url) {
        Page page = null;
        CloseableHttpClient client = HttpClients.createDefault();
        // 发送get请求
        HttpGet request = new HttpGet(url);
        request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36");
//        request.setHeader("Referer", "https://chem.usthk.cn/sites/chem-prod.sites2.ust.hk/files/css/css_Nb8nb8uZoH_0nRpwz5EuUOO3eMS9QZhpbWaJlV-E6K4.css?raznny");
//        request.setHeader("Host", "chem.usthk.cn");
        request.setConfig(requestConfig);
        try {
            CloseableHttpResponse response = client.execute(request);
            // 请求发送成功，并得到响应
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                page = new Page(EntityUtils.toByteArray(entity), url, entity.getContentType().toString()); //封装成为页面
            }

        } catch (IOException e) {
            throw new RuntimeException("get请求提交失败", e);
        } finally {
            request.releaseConnection();
        }
        return page;
    }


    public static Page sendRequstAndGetResponseHk(String url) {
        Page page = null;
        CloseableHttpClient client = HttpClients.createDefault();
        // 发送get请求
        HttpGet request = new HttpGet(url);
        request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36");
        request.setHeader("Cookie", "PHPSESSID=3r4vhmc0km0vb12e6bu4r3ego1");
        request.setHeader("Host", "chem.usthk.cn");
        request.setConfig(requestConfig);
        try {
            CloseableHttpResponse response = client.execute(request);
            // 请求发送成功，并得到响应
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                page = new Page(EntityUtils.toByteArray(entity), url, entity.getContentType().toString()); //封装成为页面
            }

        } catch (IOException e) {
            throw new RuntimeException("get请求提交失败", e);
        } finally {
            request.releaseConnection();
        }
        return page;
    }
}